package com.chinairi.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chinairi.common.annotation.DoPage;
import com.chinairi.common.utils.UserSession;
import com.chinairi.mall.controller.viewobject.ContractMatAllVO;
import com.chinairi.mall.model.ContractMat;
import com.chinairi.mall.dao.ContractMatMapper;
import com.chinairi.mall.model.SysUser;
import com.chinairi.mall.service.IContractMatService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chinairi.mall.service.modelobject.ViewContractMatMO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * <p>
 * 合同_物料表 服务实现类
 * </p>
 *
 * @author Webgasd
 * @since 2020-09-03
 */
@Service
public class ContractMatServiceImpl extends ServiceImpl<ContractMatMapper, ContractMat> implements IContractMatService {
    @Autowired
    private ContractMatMapper contractMatMapper;

    /**
     * 获取物料维度的所有配额。
     *
     * @return list
     */
    @Override
    @DoPage
    public List<ViewContractMatMO> getList(ViewContractMatMO search) {
        return contractMatMapper.getList(search);
    }

    /**
     * 更新列表中的年度需求后将所有合同中的年度总量都修改。
     *
     * @param viewContractMatMO
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateMatQty(ViewContractMatMO viewContractMatMO) {
        List<ContractMat> contractMatList = contractMatMapper.selectIdList(viewContractMatMO.getMatDbid());
        if (contractMatList.size() > 0) {
            contractMatMapper.batchUpdate(contractMatList.stream().peek((list) -> {
                list.setCmMatQty(viewContractMatMO.getCmMatQty());
            }).collect(Collectors.toList()));
        }
    }

    /**
     * 获取物料维度的所有配额。
     *
     * @return list
     */
    @Override
    @DoPage
    public List<ContractMatAllVO> selectByMatAllBpList(String code) {
        return contractMatMapper.selectByMatAllBpList(code);
    }

    /**
     * 更新物料的配额和价格
     *
     * @param contractMatAllVO
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateMatPercentageOrPrice(ContractMatAllVO contractMatAllVO) {
        ContractMat contractMat = new ContractMat();
        BeanUtils.copyProperties(contractMatAllVO, contractMat);
        contractMatMapper.updateById(contractMat);
    }
}
